
class LayerUtils:
    
    # Version of layer change commands we used. 
    # The 0 index is the currently used ones, below are older variations
    LayerChangeCommands = [
        "M118 E1 OCTOAPP_LAYER",
        "M118 E1 OCTOAPP_LAYER_CHANGE",
        "OCTOAPP_LAYER_CHANGE"
    ]
    
    # Version of disable legacy layer commands we used. 
    # The 0 index is the currently used ones, below are older variations
    DisableLegacyLayerCommands = [
        "M118 E1 OCTOAPP_DISABLE_LAYER_MAXIC",
        "M118 E1 OCTOAPP_DISABLE_LAYER_MAGIC",
        "OCTOAPP_DISABLE_LAYER_MAGIC"
    ]


    @staticmethod
    def CreateLayerChangeCommands(layer):
        return list(map(lambda x: x + " LAYER=" + str(layer), LayerUtils.LayerChangeCommands))
    
    @staticmethod
    def IsOctoAppCommand(cmd):
        return cmd.startswith("M118 E1 OCTOAPP_") or cmd.startswith("OCTOAPP_")
    

    @staticmethod
    def IsLayerChange(line, context):
        if line.startswith("; generated by PrusaSlicer") or line.startswith("; generated by OrcaSlicer") or line.startswith("; generated by SuperSlicer"):
            context["slicer"] = "prusa"

        if line.startswith("; BambuStudio"):
            context["slicer"] = "bambu"

        if line.startswith(";Generated with Cura"):
            context["slicer"] = "cura"

        if line.startswith(";Sliced by ideaMaker"):
            context["slicer"] = "ideaMaker"

        if line.startswith("; generated by Slic3r"):
            context["slicer"] = "slic3r" # Doesn't mark layer changes

        if line.startswith("; Generated by Kiri:Moto"):
            context["slicer"] = "kirimoto"

        if line.startswith("; G-Code generated by Simplify3D"):
            context["slicer"] = "simplify"

        # Artillery Slicer (and others?) removed the "generated by" line, this line indicates a PS derivative
        if line.startswith("; external perimeters extrusion width ="):
            context["slicer"] = "prusa"
        
        # Second fallback, this indicates a Cura derivative
        if line.startswith(";LAYER_COUNT:"):
            context["slicer"] = "cura"

        slicer = context.get("slicer", None)

        if slicer == "prusa":
            return line.startswith(";LAYER_CHANGE")
        
        if slicer == "bambu":
            return line.startswith("; CHANGE_LAYER")
        
        if slicer == "cura":
            return line.startswith(";LAYER:") 
    
        if slicer == "ideaMaker":
            return line.startswith(";LAYER:") 
        
        if slicer == "kirimoto":
            return line.startswith(";; --- layer")
        
        if slicer == "simplify":
            return line.startswith("; layer ") 
        
        return line.startswith("; OCTOAPP_LAYER_CHANGE") 